package com.study_platform.controller;


import com.alibaba.fastjson.JSONObject;
import com.study_platform.entity.Pk;
import com.study_platform.service.PkService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/study_platform/pk")
public class PkController {
    @Resource
    private PkService pkService;
    @PostMapping("/start/")
    public String startGame(@RequestParam MultiValueMap<String, String> data) {
        Integer aId = Integer.parseInt(Objects.requireNonNull(data.getFirst("a_id")));
        Integer bId = Integer.parseInt(Objects.requireNonNull(data.getFirst("b_id")));
        Integer grade = Integer.valueOf(Objects.requireNonNull(data.getFirst("grade")));
        String point = data.getFirst("point");
        return pkService.startGame(aId, bId, grade, point);
    }

    @GetMapping("/get/{user_id}/")
    public JSONObject get(@PathVariable Integer user_id){
        List<Pk> pks = pkService.lambdaQuery()
                .orderByDesc(Pk::getPkId)
                .eq(Pk::getUser1Id, user_id)
                .or()
                .eq(Pk::getUser2Id, user_id)
                .list();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pks", pks);
        return jsonObject;
    }
}

